package client.comm.baoding.dialog

import android.util.Log
import android.view.View
import android.widget.LinearLayout
import androidx.databinding.Observable
import androidx.databinding.ObservableField
import androidx.lifecycle.MutableLiveData
import client.comm.commlib.base.BaseActivity
import client.comm.baoding.R
import client.comm.baoding.api.bean.GoodsDetail
import client.comm.baoding.api.bean.GoodsGylDetail
import client.comm.baoding.databinding.DialogSelectgylskuBinding
import client.comm.baoding.databinding.DialogSelectskuBinding
import client.comm.baoding.utils.NavigationBar1.getBottomNavigatorHeight
import client.comm.baoding.utils.StatusBarUtil4
import client.comm.commlib.comm_ui.bean.SkuObj
import client.comm.commlib.dipToPx
import client.comm.commlib.toast.toast
import client.tzstore.oo.dialog.BottomBindingDialog
import java.math.BigDecimal
import kotlin.properties.Delegates

class SelectGylSkuDialog(val context: BaseActivity<*, *>, val type: String) :
    BottomBindingDialog<DialogSelectgylskuBinding>(context) {
    val activity: BaseActivity<*, *>

    lateinit var id: String
    var price by Delegates.notNull<Double>()

    lateinit var goods: GoodsGylDetail

    lateinit var imgUrl: String
    lateinit var name: String

    val selectNum = ObservableField<String>("1").apply {
        addOnPropertyChangedCallback(object : Observable.OnPropertyChangedCallback() {
            override fun onPropertyChanged(sender: Observable?, propertyId: Int) {
                sender?.let {
                    it as ObservableField<String>
                    var num = 0
                    try {
                        if (!it.get().isNullOrEmpty())
                            num = it.get()!!.toInt()

                        selectSku.value?.let {
                            var p =
                                BigDecimal("${it.goodsMarkprice}").multiply(
                                    BigDecimal("$num")
                                )
                            binding.tvPrice.text =
                                ("￥") + "${p}"
                        }

                    } catch (e: Exception) {
                    }

                    binding.reducueTv.isEnabled = num > 1
                }
            }
        })
    }

    init {
        activity = context
    }

    override fun getV(): Int {
        return R.layout.dialog_selectgylsku
    }

    val selectSku by lazy {
        MutableLiveData<SkuObj>()
    }

    override fun postBinding() {
        binding.event = this
        window?.setWindowAnimations(R.style.dialogWindowAnimButtomToTop);
        window?.setBackgroundDrawableResource(android.R.color.transparent)

        activity.getImgLoader().load(imgUrl).into(binding.img)
        if (type.isNotEmpty() && type.equals("1")) {

            var layout = binding.btnOk.layoutParams as LinearLayout.LayoutParams
            if (!StatusBarUtil4.isNavBarHide(context)) {
                layout.bottomMargin =
                    getBottomNavigatorHeight(context) + dipToPx(context.resources!!, 20.0f).toInt()
            } else {
                layout.bottomMargin = 0 //将默认的距离底部改为0
            }
            binding.btnOk.layoutParams = layout
//            binding.flDialogSku.setPadding(0, 0, 0, dipToPx(context.resources!!, 50.0f).toInt())
        } else {

        }

//        if (goods.price!! > 0) {
//            binding.tvPrice.text = "￥${goods.price}"
//        } else if (goods.integral!! > 0) {
//            binding.tvPrice.text = "积分${goods.integral}"
//        } else if (goods.medal_price!! > 0) {
//            binding.tvPrice.text = "勋章${goods.medal_price}"
//        }

        binding.skuWrap.apply {

            selectSkuChange = {
                if (it != null) {
                    Log.d("DDD", it.goodsSpec)
                    binding.tvSku.text = "已选:${it!!.goodsSpec}"
                    var p =
                        BigDecimal("${it.goodsMarkprice}").multiply(
                            BigDecimal(selectNum.get())
                        )
                    binding.tvPrice.text = ("￥") + "${p}"
                    activity.getImgLoader().load(it.goodsImage).into(binding.img)

                } else {
                    binding.tvSku.text = "规格:请选择"
                    binding.tvPrice.text = "￥${goods.price}"
                    activity.getImgLoader().load(imgUrl).into(binding.img)

                }
                binding.btnEn = it != null
                selectSku.value = (it)
            }
        }.setData(ArrayList<SkuObj>().apply {

            goods.goods_sku_list.forEach {

                val s = it.sku_spec.joinToString(";") { o ->
                    "${o.name}:${o.value}"
                }
                add(
                    SkuObj(
                        id = "${it.id}",
                        goodsId = "${it.goods_id}",
                        spuCode = "$it.spu_code",
                        goodsMarkprice = it.price,
                        status = it.status,
                        goodsSpec = s,
                        goodsImage = it.sku_img,
                        goodsName = goods.name,
                        deliveryType = 5,
                        medalNum = it.medal_num
                    )
                )
            }

        })
    }

    fun changeNum(n: Int) {
        var num = 0
        try {
            num = selectNum.get()!!.toInt()
        } catch (e: Exception) {
        }
        num += n
        if (num < 1)
            num = 1
        selectNum.set("$num")
    }

    var ok: ((String, SkuObj?) -> Unit)? = null

    fun queren() {
        if (selectSku.value == null) {
            toast("请选择规格后操作")
            return
        }

        cancel()
        ok?.invoke(selectNum.get() as String, selectSku.value)
    }
}